/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.beans.admin;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
import manager.CityManager;
import model.admin.Citys;
import model.admin.Country;

/**
 *
 * @author Danh
 */
@ManagedBean
@RequestScoped
public class CityBean {

    /**
     * Creates a new instance of CityBean
     */
    private int Country_Id;
    private String City_Name;
    private int City_Id;
    private List<Country> listCountry;
    Connection conn;
    private List<Integer> list;

    public List<Integer> getList() {
        return list;
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }

    public List<Country> getCountry() {
        try {
            listCountry = new ArrayList<Country>();
            conn = MyConn.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("SELECT Country_Id,Country_Name FROM Countries");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Country c = new Country();
                c.setCountryId(rs.getInt("Country_Id"));
                c.setCountryName(rs.getNString("Country_Name"));
                listCountry.add(c);
            }
        } catch (Exception e) {
        }

        return listCountry;
    }
    public void valueChange(ValueChangeEvent e)
    {
    int selectCountry;
        selectCountry =Integer.parseInt(e.getNewValue().toString());
    }

    public void reset() {
        City_Name = null;
    }

    public String editAction(int id, String name) {
        this.City_Id = id;
        this.City_Name = name;
        return "updateCity.xhtml";
    }

    public CityBean(int Country_Id, String City_Name, int City_Id) {
        this.Country_Id = Country_Id;
        this.City_Name = City_Name;
        this.City_Id = City_Id;
    }

    public CityBean() {
    }

    public int getCountry_Id() {
        return Country_Id;
    }

    public void setCountry_Id(int Country_Id) {
        this.Country_Id = Country_Id;
    }

    public String getCity_Name() {
        return City_Name;
    }

    public void setCity_Name(String City_Name) {
        this.City_Name = City_Name;
    }

    public int getCity_Id() {
        return City_Id;
    }

    public void setCity_Id(int City_Id) {
        this.City_Id = City_Id;
    }
    private boolean editable = false;

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public List<Citys> loadCity() {
        CityManager mana = new CityManager();
        return mana.getCity();

    }

    public void deleteCity(int id) {
        CityManager mana = new CityManager();
        boolean deleteCity = mana.deleteCity(id);
        if (deleteCity) {
            loadCity();
        }
    }

    public String sua() {

        Citys city = new Citys(this.City_Id, City_Name, Country_Id);
        CityManager mana = new CityManager();
        mana.update(city);
        return "citiesManager.xhtml?faces-redirect=true";
    }

    public void insertCity() {
        Citys c = new Citys(City_Id, City_Name, Country_Id);
        CityManager mana = new CityManager();
        boolean result = mana.insertCity(c);
        if (result) {
            loadCity();
        }
    }

    public static void main(String[] args) {
        CityBean c = new CityBean();
        //  Citys city=new Citys(Country_Id, null, City_Id)
        c.sua();
    }
}
